﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class SJDao
    {


        public SJDao()
        {

            Mapper.CreateMap<Y_SJ, SJModel>();
        }
        public List<Y_SJ> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_SJ.Count();

                return edm.Y_SJ.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public SJModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_SJ.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_SJ, SJModel>(query);
            }

        }


        public int Save(SJModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_SJ.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_SJ();

                    edm.AddToY_SJ(query);

                }


                query.ProjectId = obj.ProjectId;

                query.fasj = obj.fasj;

                query.fasjother = obj.fasjother;

                query.dzkc = obj.dzkc;

                query.dzkcother = obj.dzkcother;

                query.cbsj = obj.cbsj;

                query.cbsjother = obj.cbsjother;

                query.sgt = obj.sgt;

                query.sgtother = obj.sgtother;

                query.sgtsc = obj.sgtsc;

                query.sgtscother = obj.sgtscother;

                query.sgtbg = obj.sgtbg;

                query.sgtbgother = obj.sgtbgother;

                query.gcjs = obj.gcjs;
                query.gcsf = obj.gcsf;
                query.gcys = obj.gcys;
                query.jggl = obj.jggl;
                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;
                query.createrealname = obj.createrealname;
                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }
        /// <summary>
        ///  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SJModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_SJ.Where(s => s.ProjectId == id&&s.Id==reid);
                

                return Mapper.Map<Y_SJ, SJModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        public SJModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                return Mapper.Map<Y_SJ, SJModel>(edm.Y_SJ.Where(s=>s.ProjectId==id).OrderByDescending(s=>s.Id).FirstOrDefault());
            }
        }

        /// <summary>
        /// 获取所有的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<SJModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_SJ.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_SJ>, List<SJModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_SJ.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

